#!/usr/bin/env ruby
require 'rubygems'
require 'net/toc'

client = Net::TOC.new("RPIEventsBuddy","tennis")
client.connect
puts "connected\n"

people = Hash.new
phones = Hash.new

client.on_im do |message,buddy|
	message.gsub!(/<\/?[^>]*>/,"")
	puts "#{buddy.screen_name}: #{message}"
	
	
	unless people[buddy]
		people[buddy] = 0
	end
	
	if people[buddy] == 0
		unless phones[buddy]
			buddy.send_im "Hey can I have yo numba?"
			puts "To #{buddy.screen_name}: Hey can I have yo numba?"
		else
			buddy.send_im "Hey, yo numba is #{phones[buddy]}?"
			puts "To #{buddy.screen_name}: Hey, yo numba is #{phones[buddy]}?"
			people[buddy] = 2
		end
		people[buddy] = 1
	elsif people[buddy] == 1 and (m = /.*(\d{3}).*(\d{3}).*(\d{4})/.match(message))
		unless phones[buddy]
			buddy.send_im "Yo numba is (#{m.captures[0]})-#{m.captures[1]}-#{m.captures[2]}?"
			puts "To #{buddy.screen_name}: Yo numba is (#{m.captures[0]})-#{m.captures[1]}-#{m.captures[2]}?"
			phones[buddy] = "(#{m.captures[0]})-#{m.captures[1]}-#{m.captures[2]}"
		else
			buddy.send_im "Yo new numba is (#{m.captures[0]})-#{m.captures[1]}-#{m.captures[2]}?"
			puts "To #{buddy.screen_name}: Yo new numba is (#{m.captures[0]})-#{m.captures[1]}-#{m.captures[2]}?"
			phones[buddy] = "(#{m.captures[0]})-#{m.captures[1]}-#{m.captures[2]}"
		end
	elsif people[buddy] == 1 and (m = /yes|ya|yeah|yup/.match(message))
		buddy.send_im "Aight say goodbye, I'll call ya lata"
		puts "To #{buddy.screen_name}: Aight say goodbye, I'll call ya lata"
		people[buddy] = 2
	elsif people[buddy] == 1 and (m = /no|na/.match(message))
		buddy.send_im "Whats yo numba?"
		puts "To #{buddy.screen_name}: Whats yo new digits?"
	elsif people[buddy] == 1
		buddy.send_im "I said can I have yo numba??"
		puts "To #{buddy.screen_name}: I said can I have yo numba??"
	elsif people[buddy] == 2 and (m = /bye/.match(message))
		buddy.send_im "Peace out!"
		puts "To #{buddy.screen_name}: Peace out!"
		people[buddy] = 0
	elsif people[buddy] == 2
		buddy.send_im "Dude say bye, I'm out."
		puts "To #{buddy.screen_name}: Dude say bye, I'm out."
	end
end

client.on_error do |error|
	puts "!! #{error}"
end

client.wait